package admin;

import java.util.*;

import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.actions.*;
import model.*;

public class CustomAction extends DispatchAction{
	
	
	//회원의 가입수,가입자 아이디 취득
	public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception {
		
		//Form취득
		CustomForm cusform = (CustomForm)form;
		
		//전체 회원수
		int totalCount = 0;
		
		//회원 정보
		Vector<Member> mVector = null;
		
		//멤버 테이블 매칭 DAO
		MemberDAO memDao = new MemberDAO();
		
		//******************************필요테이터취득
		totalCount = memDao.countMembers();
		mVector = memDao.getMembers();
		//******************************완료
		
		cusform.setSearchMember_select("");
		cusform.setTotalCount(""+totalCount);
		cusform.setMembers(mVector);
		
		return mapping.findForward("form");
	}
	
	
	//입력된 아이디로 회원정보 취득
	public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception {
		
		//Form취득
		CustomForm cusform = (CustomForm)form;
		//전체 회원수
		String totalCount = cusform.getTotalCount();
		//회원 정보
		Vector<Member> mVector = null;
		//회원레벨
		Vector<MemberLevel> mlvVector = null;
		
		//******************************필요테이터취득
		//멤버 테이블 매칭 DAO
		MemberDAO memDao = new MemberDAO();
		MemberLevelDAO memLvDao = new MemberLevelDAO();
		//=================================================
		totalCount = String.valueOf(memDao.countMembers());
		mVector = memDao.getMembers();
		mlvVector = memLvDao.getList();
		//******************************완료
		
		//검색방법 분기 *********************************************************
		String searchkind = req.getParameter("searchkind");
		
		//선택검색
		if(searchkind.equals("0"))
		{
			//일반검색
			Vector<Member> member;
			member = memDao.getMemberById(cusform.getSearchMember_select());
			
			cusform.setMember(member);
			cusform.setSearchMember_input("");
			
			if(member.get(0).getLvno() == 9)
			{
				//최고 관리자의경우 최고관리자 라는 정보를 보여준다.
				mlvVector = memLvDao.getList_full();
			}
		}
		else //입력검색
		{
			//이름검색
			String fromName = req.getParameter("fromName");
			
			Vector<Member> member;
			
			
			//이름 검색
			if(fromName != null)
			{
				member = memDao.getMemberByName(cusform.getSearchMember_input());
			}
			else
			{
				member = memDao.getMemberById(cusform.getSearchMember_input());
			}
			
			if(member.isEmpty())
			{
				cusform.setMember(null);
			}
			else
			{
				if(member.get(0).getLvno() == 9)
				{
					//최고 관리자의경우 최고관리자 라는 정보를 보여준다.
					mlvVector = memLvDao.getList_full();
				}
				cusform.setMember(member);
			}
		}
		//*************************************************************************
		
		cusform.setTotalCount(totalCount);
		cusform.setMembers(mVector);
		cusform.setMemberLv(mlvVector);
		return mapping.findForward("form");
	}

	//수정된 레벨로 해당 회원 갱신
	public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception {
		
		//2007년 5월 29일
		//갱신처리 부분 완성.
		
		//Form취득
		CustomForm cusform = (CustomForm)form;
		
		//전체 회원수
		String totalCount = cusform.getTotalCount();
		
		//회원 정보
		Vector<Member> mVector = null;
		
		//회원레벨
		Vector<MemberLevel> mlvVector = null;
		
		//멤버 테이블 매칭 DAO
		MemberDAO memDao = new MemberDAO();
		MemberLevelDAO memLvDao = new MemberLevelDAO();
		
		//******************************필요테이터취득
		//totalCount = memDao.countMembers();
		mVector = memDao.getMembers();
		mlvVector = memLvDao.getList();
		//******************************완료
		
		
		//******************************레벨 업데이트
		String mid = cusform.getMid();
		int lvno = Integer.valueOf(cusform.getLevel());
		
		System.out.println(mid);
		System.out.println(lvno);
		
		MemberCommunityDAO MemCommDao = new MemberCommunityDAO();
		//관리자
		if(lvno == 8)
		{
			System.out.println("관리업데이트");
			MemCommDao.insert(mid, lvno, "관리자");
		}
		else
		{
			//기존 관리자 레코드를 삭제한다.
			MemCommDao.delete(mid, 2);
			MemCommDao.delete(mid, 5);
			MemCommDao.delete(mid, 6);
			MemCommDao.delete(mid, 7);
			MemCommDao.delete(mid, 8);
		}
		
		Member member = new Member();
		member.setMid(mid);
		member.setLvno(lvno);
		int a = memDao.updateLvno(member);
		System.out.println(a);
		
		//*********************************************
		cusform.setTotalCount(totalCount);
		
		//개인회원 정보설정******************************************************************
		cusform.setMember(memDao.getMemberById(cusform.getMid())); 	//개인회원정보
		cusform.setMembers(mVector);								//회원들정보(아이디,이름)
		cusform.setMemberLv(mlvVector);								//레벨정보
		//***********************************************************************************
		
		return mapping.findForward("form");
	}
}
